﻿using System;
using Orchard.Caching;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Extensions;
using Rework.CommerceHistory.Models;

namespace Rework.CommerceHistory.Drivers {
    [OrchardFeature("Rework.CommercePayPal")]
    public class CommercePayPalSettingsPartDriver : ContentPartDriver<CommercePayPalSettingsPart> {
        private readonly ISignals _signals;

        public CommercePayPalSettingsPartDriver(ISignals signals) {
            _signals = signals;
        }

        protected override string Prefix { get { return "PayPalSettings"; } }

        protected override DriverResult Editor(CommercePayPalSettingsPart part, dynamic shapeHelper) {
            return ContentShape("Parts_Commerce_PayPal_Settings",
                               () => shapeHelper.EditorTemplate(
                                   TemplateName: "Parts/Commerce.PayPalSettings",
                                   Model: part.Record,
                                   Prefix: Prefix)).OnGroup("PayPalSettings");
        }

        protected override DriverResult Editor(CommercePayPalSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(part.Record, Prefix, null, null);
            _signals.Trigger("PayPalSettings.Changed");
            return Editor(part, shapeHelper);
        }

        protected override void Importing(CommercePayPalSettingsPart part, ImportContentContext context) {
            var merchantId = context.Attribute(part.PartDefinition.Name, "MerchantId");
            if (!String.IsNullOrWhiteSpace(merchantId)) {
                part.MerchantId = merchantId;
            }
            var currency = context.Attribute(part.PartDefinition.Name, "Currency");
            if (!String.IsNullOrWhiteSpace(currency)) {
                part.Currency = currency;
            }
            var weightUnit = context.Attribute(part.PartDefinition.Name, "WeightUnit");
            if (!String.IsNullOrWhiteSpace(weightUnit)) {
                part.WeightUnit = weightUnit;
            }
            var businessEmailAddress = context.Attribute(part.PartDefinition.Name, "BusinessEmailAddress");
            if (!String.IsNullOrWhiteSpace(businessEmailAddress)) {
                part.BusinessEmailAddress = businessEmailAddress;
            }
            var pdtAuthenticationToken = context.Attribute(part.PartDefinition.Name, "PDTAuthenticationToken");
            if (!String.IsNullOrWhiteSpace(pdtAuthenticationToken)) {
                part.PDTAuthenticationToken = pdtAuthenticationToken;
            }
            var isSandboxAttribute = context.Attribute(part.PartDefinition.Name, "IsSandbox");
            bool isSandbox;
            if (Boolean.TryParse(isSandboxAttribute, out isSandbox)) {
                part.IsSandbox = isSandbox;
            }
        }

        //protected override void Exporting(GoogleCheckoutSettingsPart part, ExportContentContext context) {
        //    context.Element(part.PartDefinition.Name).SetAttributeValue("MerchantId", part.MerchantId);
        //    context.Element(part.PartDefinition.Name).SetAttributeValue("Currency", part.Currency);
        //    context.Element(part.PartDefinition.Name).SetAttributeValue("WeightUnit", part.WeightUnit);
        //    context.Element(part.PartDefinition.Name).SetAttributeValue("AnalyticsId", part.AnalyticsId);
        //    context.Element(part.PartDefinition.Name).SetAttributeValue("UseSandbox", part.UseSandbox.ToString().ToLower());
        //}
    }
}
